package com.tenement.controller.foreground.user;

import javax.servlet.http.HttpSession;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.tenement.base.constants.SysmConstants.existed;
import com.tenement.base.constants.SysmConstants.state;
import com.tenement.base.exception.ParameterException;
import com.tenement.service.interfaces.IUserService;

@Controller
public class UserController {

	private static final Log logger = LogFactory.getLog(UserController.class);

	@Autowired
	private IUserService iUserService;

	/**
	 * 判断邮箱是否已经被占用了或账号是否存在
	 * 
	 * @param email
	 * @return
	 */
	@RequestMapping(value = "/verifyEmailUsed.action")
	public @ResponseBody String verifyEmailUsed(@RequestBody String email) {
		logger.info("前台传来的email【-" + email + "-】");
		boolean b = iUserService.verifyEmailUsed(email);
		if (b) {
			return existed.Yes;
		} else {
			return existed.No;
		}
	}

	/**
	 * 用户修改密码下一步，进入修改密码页面，将用户输入的邮箱存到session
	 * 
	 * @param userEmail
	 * @param session
	 * @return
	 */
	@RequestMapping(value = "modeUserPwd2.action")
	public String modeUserPwd2(String userEmail, HttpSession session) {
		logger.info("【保存用户邮箱-修改密码】" + userEmail);
		session.setAttribute("userEmail", userEmail);
		return "redirect:view/foreground/user/user_resetpwd2.html";
	}

	/**
	 * 修改用户密码
	 * 
	 * @param pwd
	 * @param session
	 * @return
	 */
	@RequestMapping(value = "modifyPwd.action")
	public @ResponseBody String modifyPwd(@RequestBody String data) {
		try {
			if (data == null || "".equals(data)) {
				throw new ParameterException("【-传入的参数为空pwd-】");
			}
			JSONObject json = JSON.parseObject(data);
			String pwd = json.getString("pwd");
			String uId = json.getString("uId");
			boolean b = iUserService.modifyPwd(uId, pwd);
			if (b) {
				return state.Success;// 修改成功
			} else {
				return state.Failed;// 修改失败
			}
		} catch (ParameterException e) {
			e.printStackTrace();
		}
		return state.Failed;// 修改失败
	}
}
